package Gather;

import com.dycong.common.common.SerializableUtils;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import org.junit.Test;
import tmp.Animal;
import tmp.House;

import java.io.*;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by Duke on 2017/1/22.
 */
public class testSerializable {
    @Test
    public void test() {
        new ByteInputStream();
    }

    public static List[] splitListByStep(List list, int step) {
        int count = list.size() / step;
        if (count == 0) {
            return new List[]{list};
        }
        List[] lists;

        if (count * step < list.size())
            lists = new List[count + 1];
        else
            lists = new List[count];

        for (int i = 0; i < count; ) {
            lists[i] = list.subList(i * step, (++i) * step);
        }
        if (count * step < list.size()) {
            lists[count] = list.subList(count * step, list.size());
        }
        return lists;
    }

//    public static void main(String[] args) {
//        List<Integer> list= Stream.generate(()->{return new Random().nextInt();}).limit(100).collect(Collectors.toList());
//        List[] lists=splitListByStep(list,10);
//        System.out.println("");
//    }

    /**
     *我们可以实现对任何可Serializable对象的”深度复制（deep copy）"
     * ——这意味着我们复制的是整个对象网，而不仅仅是基本对象及其引用。
     * 对于同一流的对象，他们的地址是相同，说明他们是同一个对象，但是与其他流的对象地址却不相同,
     *
     * todo 从同一个输入流里读取的相同Serializable对象，他们的地址是相同，说明他们是同一个对象。 == ：true
     * todo 从不同一个输入流里读取的相同Serializable对象，他们的地址是不相同，说明他们不是同一个对象。 == ：false
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        House house = new House();
        System.out.println("序列化前");
        Animal animal = new Animal("Gather.testSerializable", house);
        System.out.println(animal);
        String fileName = "C:/Users/dycong/Desktop/tmp/out.out";
        String fileName1 = "C:/Users/dycong/Desktop/tmp/out1.out";

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName))) {
            oos.writeObject(animal);
            oos.writeObject(animal);
            oos.flush();  //缓冲流
        }

        SerializableUtils.writeObject(animal,fileName1);
        try (ObjectInputStream oin = new ObjectInputStream(new FileInputStream(fileName))) {
            Animal animal1 = (Animal) oin.readObject();
            Animal animal2 = (Animal) oin.readObject();
            oin.close();

         Animal animal3 = (Animal) SerializableUtils.readObject(fileName1);

//2.
//        SerializableUtils.writeObject(animal,fileName);
//        SerializableUtils.writeObject(animal,fileName1);
//
//        Animal animal1=(Animal) SerializableUtils.readObject(fileName);
//        Animal animal2=(Animal) SerializableUtils.readObject(fileName);
//        Animal animal3=(Animal) SerializableUtils.readObject(fileName1);


//3.
//        ByteArrayOutputStream out = new ByteArrayOutputStream();
//        ObjectOutputStream oos = new ObjectOutputStream(out);
//        oos.writeObject(animal);
//        oos.writeObject(animal);//在写一次，看对象是否是一样，
//        oos.flush();
//        oos.close();
//
//        ByteArrayOutputStream out2 = new ByteArrayOutputStream();//换一个输出流
//        ObjectOutputStream oos2 = new ObjectOutputStream(out2);
//        oos2.writeObject(animal);
//        oos2.flush();
//        oos2.close();
//
//        System.out.println("反序列化后");
//        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
//        ObjectInputStream ois = new ObjectInputStream(in);
//        Animal animal1 = (Animal)ois.readObject();
//        Animal animal2 = (Animal)ois.readObject();
//        ois.close();
//
//        ByteArrayInputStream in2 = new ByteArrayInputStream(out2.toByteArray());
//        ObjectInputStream ois2 = new ObjectInputStream(in2);
//        Animal animal3 = (Animal)ois2.readObject();
//        ois2.close();

            System.out.println("out流：" + animal1);
            System.out.println("out流：" + animal2);
            System.out.println("out2流：" + animal3);


            System.out.println("测试序列化前后的对象 == ：" + (animal == animal1));
            System.out.println("测试序列化后同一流的对象：" + (animal1 == animal2));
            System.out.println("测试序列化后不同流的对象==:" + (animal1 == animal3));

            System.out.println("测试序列化前后的对象 == ：" + (animal.equals(animal1)));
            System.out.println("测试序列化后同一流的对象：" + (animal1.equals(animal2)));
            System.out.println("测试序列化后不同流的对象==:" + (animal1.equals(animal3)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
